package com.itheima.tlias84.controller;

import com.itheima.tlias84.dto.SelectStudentDTO;
import com.itheima.tlias84.po.InsertStudentsParam;
import com.itheima.tlias84.po.SelectStudentsPageParam;
import com.itheima.tlias84.po.UpdateStudentParam;
import com.itheima.tlias84.po.ViolationParam;
import com.itheima.tlias84.service.impl.StudentsServiceImpl;
import com.itheima.tlias84.utils.ResultUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@Slf4j
@RestController
@RequestMapping("/students")
public class StudentsController {

    @Resource
    StudentsServiceImpl studentsService;

    @GetMapping
    public ResultUtil selectAllStudents(SelectStudentsPageParam param){
        if (Objects.isNull(param.getPage()) || Objects.isNull(param.getPageSize())) {
            return ResultUtil.fail("分页参数不存在");
        }
        return studentsService.selectAllStudents(param);
    }
    @DeleteMapping("/{ids}")
    public ResultUtil deleteById(@PathVariable("ids") List<Integer> ids){
        int i = studentsService.deleteById(ids);
        return i == 0 ? ResultUtil.fail("失败") : ResultUtil.success("成功");
    }

    @PostMapping
    public ResultUtil insertStudents(@RequestBody InsertStudentsParam param){
        int i = studentsService.insertStudents(param);
        return i == 0 ? ResultUtil.fail("失败") : ResultUtil.success("成功");
    }
    @GetMapping("/{id}")
    public ResultUtil selectStudentById(@PathVariable("id") Integer id){
        SelectStudentDTO selectStudentDTO = studentsService.selectStudentById(id);
        return ResultUtil.success(selectStudentDTO);
    }

    @PutMapping
    public ResultUtil updateStudent(@RequestBody UpdateStudentParam param){
        int i = studentsService.updateStudent(param);
        return i == 0 ? ResultUtil.fail("失败") : ResultUtil.success("成功");
    }

    @PutMapping("/violation/{id}/{score}")
    public ResultUtil updateViolation(@PathVariable("id") Integer id,@PathVariable("score") Integer score){
        int i = studentsService.updateViolation(id,score);
        return i == 0 ? ResultUtil.fail("失败") : ResultUtil.success("成功");
    }
}
